package ups.apps.converter;

import java.util.List;

import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import ups.apps.model.Cliente;

@FacesConverter("ClienteConverterCuenta")
public class ClienteConverterCuenta implements Converter {
	@Override
		public Object getAsObject(FacesContext context, UIComponent component,
				String value) {
			List<Cliente> clientes= this.getDAO();
			for(Cliente c : clientes){
				if (c.getCliCedula().equals(value))
					return c;
			}
			return null;		
		}

		@Override
		public String getAsString(FacesContext context, UIComponent component,
				Object value) {
				
			return ((Cliente) value).getCliCedula();
		}
		
		public List<Cliente> getDAO(){				
			FacesContext ctx = FacesContext.getCurrentInstance();
			ELContext ec = ctx.getELContext();
			Application app = ctx.getApplication();
			return (List) app.evaluateExpressionGet(ctx,"#{cuentaBean.lista}",List.class);		
		}


}
